﻿/*************************************************************************/
/** Copyright.															**/
/** FileName: List.c													**/
/** Author: Polarix														**/
/** Description: HMI demo for list control interface.					**/
/*************************************************************************/
//=======================================================================//
//= Include files.													    =//
//=======================================================================//
#include "app_gui.h"
#include "SGUI_List.h"
#include "SGUI_FontResource.h"
#include "Resource.h"

//=======================================================================//
//= User Macro definition.											    =//
//=======================================================================//

//=======================================================================//
//= Static function declaration.									    =//
//=======================================================================//
static HMI_ENGINE_RESULT	HMI_Menu_Initialize(SGUI_SCR_DEV* pstDeviceIF);
static HMI_ENGINE_RESULT	HMI_Menu_Prepare(SGUI_SCR_DEV* pstDeviceIF, const void* pstParameters);
static HMI_ENGINE_RESULT	HMI_Menu_RefreshScreen(SGUI_SCR_DEV* pstDeviceIF, const void* pstParameters);
static HMI_ENGINE_RESULT	HMI_Menu_ProcessEvent(SGUI_SCR_DEV* pstDeviceIF, const HMI_EVENT_BASE* pstEvent, SGUI_INT* piActionID);
static HMI_ENGINE_RESULT	HMI_Menu_PostProcess(SGUI_SCR_DEV* pstDeviceIF, HMI_ENGINE_RESULT eProcResult, SGUI_INT iActionID);

//=======================================================================//
//= Static variable declaration.									    =//
//=======================================================================//

static SGUI_ITEMS_ITEM		s_arrstListItems[] =		{	{SCR1_LIST_ITEM1, NULL},
															{SCR1_LIST_ITEM2, NULL},
															{SCR1_LIST_ITEM3, NULL},
//															{SCR1_LIST_ITEM4, NULL},
														};

typedef struct
{
	SGUI_ITEMS_ITEM	stItem;
	SGUI_BOOL		bUsed;
}SGUI_TEST_ITEM;

static SGUI_LIST		s_stMenuObject = 		{0x00};

//=======================================================================//
//= Global variable declaration.									    =//
//=======================================================================//
HMI_SCREEN_ACTION		s_stMenuActions =			{	HMI_Menu_Initialize,
															HMI_Menu_Prepare,
															HMI_Menu_RefreshScreen,
															HMI_Menu_ProcessEvent,
															HMI_Menu_PostProcess
														};
HMI_SCREEN_OBJECT       g_stHMI_Menu =				{	HMI_SCREEN_ID_MENU,
															&s_stMenuActions
														};

//=======================================================================//
//= Function define.										            =//
//=======================================================================//
HMI_ENGINE_RESULT HMI_Menu_Initialize(SGUI_SCR_DEV* pstDeviceIF)
{
	/*----------------------------------*/
	/* Variable Declaration				*/
	/*----------------------------------*/
	SGUI_RECT					stListLayout;	
	/*----------------------------------*/
	/* Process							*/
	/*----------------------------------*/
    // Initialize list data.
    SGUI_SystemIF_MemorySet(&s_stMenuObject, 0x00, sizeof(SGUI_LIST));
     //Initialize list object.
	stListLayout.iX = 0;
	stListLayout.iY = 0;
	stListLayout.iWidth = pstDeviceIF->stSize.iWidth;
	stListLayout.iHeight = pstDeviceIF->stSize.iHeight;
	SGUI_List_Initialize(&s_stMenuObject, &stListLayout, &GB2312_FZXS12, SCR1_TITLE, s_arrstListItems, sizeof(s_arrstListItems)/sizeof(SGUI_ITEMS_ITEM));
	return HMI_RET_NORMAL;
}

HMI_ENGINE_RESULT HMI_Menu_Prepare (SGUI_SCR_DEV* pstDeviceIF, const void* pstParameters)
{
	/*----------------------------------*/
	/* Process							*/
	/*----------------------------------*/
    SGUI_List_Selecte(&s_stMenuObject, 0);
	SGUI_List_Repaint(pstDeviceIF, &s_stMenuObject);
	BLK_State_Set(BLK_STATE_ADMIN_MODE_PASSWORD);
	return HMI_RET_NORMAL;
}

HMI_ENGINE_RESULT HMI_Menu_RefreshScreen(SGUI_SCR_DEV* pstDeviceIF, const void* pstParameters)
{
	/*----------------------------------*/
	/* Process							*/
	/*----------------------------------*/
	SGUI_List_Repaint(pstDeviceIF, &s_stMenuObject);
	return HMI_RET_NORMAL;
}

HMI_ENGINE_RESULT HMI_Menu_ProcessEvent(SGUI_SCR_DEV* pstDeviceIF, const HMI_EVENT_BASE* pstEvent, SGUI_INT* piActionID)
{
	/*----------------------------------*/
	/* Variable Declaration				*/
	/*----------------------------------*/
	HMI_ENGINE_RESULT           eProcessResult;
	SGUI_UINT16					uiKeyCode;
	SGUI_UINT16					uiKeyValue;
	KEY_PRESS_EVENT*			pstKeyEvent;
	SGUI_INT					iProcessAction;
	SGUI_SIZE					sInsertDataIdx;
	SGUI_ITEMS_ITEM*			pstItemObj;

	/*----------------------------------*/
	/* Initialize						*/
	/*----------------------------------*/
	eProcessResult =			HMI_RET_NORMAL;
	pstKeyEvent =				(KEY_PRESS_EVENT*)pstEvent;
	iProcessAction =			HMI_DEMO_PROC_NO_ACT;

	/*----------------------------------*/
	/* Process							*/
	/*----------------------------------*/
	if(pstEvent->iType == EVENT_TYPE_ACTION)
	{
		// Check event is valid.
		if(SGUI_FALSE == HMI_EVENT_SIZE_CHK(*pstKeyEvent, KEY_PRESS_EVENT))
		{
			// Event data is invalid.
			eProcessResult = HMI_RET_INVALID_DATA;
		}
		else if(EVENT_ID_KEY_PRESS == pstEvent->iID)
		{
		    uint8_t select_flag = 0;
		    uiKeyValue = pstKeyEvent->Data.uiKeyValue;

			switch(uiKeyValue)
			{
                case KEY_NUMA:
                {
                    iProcessAction = HMI_DEMO_PROC_CANCEL;
                    break;
                }
                case KEY_NUMP:
                {
                    iProcessAction = HMI_DEMO_PROC_CONFIRM;
                    break;
                }
				case KEY_NUM8:
				{
					if(SGUI_List_GetSelection(&s_stMenuObject)->iIndex > 0)
					{
					    select_flag = 1;
						SGUI_List_Selecte(&s_stMenuObject, SGUI_List_GetSelection(&s_stMenuObject)->iIndex-1);
					}
					SGUI_List_Repaint(pstDeviceIF, &s_stMenuObject);
					break;
				}
				case KEY_NUM0:
				{
					if(SGUI_List_GetSelection(&s_stMenuObject)->iIndex < (s_stMenuObject.stItems.iCount-1))
					{
					    select_flag = 1;
						SGUI_List_Selecte(&s_stMenuObject, SGUI_List_GetSelection(&s_stMenuObject)->iIndex+1);
					}
					SGUI_List_Repaint(pstDeviceIF, &s_stMenuObject);
					break;
				}
				default:
				{
					break;
				}
			}

			if(select_flag)
			{
			    select_flag = 0;
	            switch(SGUI_List_GetSelection(&s_stMenuObject)->iIndex)
	            {
	                case 0 :BLK_State_Set(BLK_STATE_ADMIN_MODE_PASSWORD);break;
	                case 1 :BLK_State_Set(BLK_STATE_ADMIN_MODE_FINGERPRINT);break;
	                case 2 :BLK_State_Set(BLK_STATE_ADMIN_MODE_CARD);break;
	                default : break;
	            }
			}

		}
	}
	if(NULL != piActionID)
	{
		*piActionID = iProcessAction;
	}

	return eProcessResult;
}

HMI_ENGINE_RESULT HMI_Menu_PostProcess(SGUI_SCR_DEV* pstDeviceIF, HMI_ENGINE_RESULT eProcResult, SGUI_INT iActionID)
{
	if(HMI_DEMO_PROC_CONFIRM == iActionID)
	{
        switch(SGUI_List_GetSelection(&s_stMenuObject)->iIndex)
        {
        	case 0:
			{
			    SGUI_UINT8      pin_mode = VARIABLE_PIN_MODE_CONFIG;
			    HMI_SwitchScreen(HMI_SCREEN_ID_PIN, &pin_mode);
				break;
			}
			case 1:
			{
                HMI_SwitchScreen(HMI_SCREEN_ID_FINGERPRINT, NULL);
				break;
			}
			case 2:
			{
                HMI_SwitchScreen(HMI_SCREEN_ID_NFCA, NULL);
				break;
			}
//			case 3:
//			{
//				break;
//			}
			default:
			{
				/* do nothing. */
			}
        }
	}
	else if(HMI_DEMO_PROC_CANCEL == iActionID)
	{
		HMI_GoBack(NULL);
	}

	return HMI_RET_NORMAL;
}

